//
//  LocationTestAppDelegate.m
//  LocationTest
//
//  Created by MAC08 on 07/05/12.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "LocationTestAppDelegate.h"
#import "LocationTestViewController.h"
#define BestAccuracyRange 85

@implementation LocationTestAppDelegate

@synthesize window;
@synthesize viewController;
@synthesize newlocation;
@synthesize objWS;

#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
    // Override point for customization after application launch.
	AppDel = (LocationTestAppDelegate *) [[UIApplication sharedApplication] delegate];
	self.objWS =[[WS alloc] init];
    // Add the view controller's view to the window and display.
    [self.window addSubview:viewController.view];
    [self.window makeKeyAndVisible];
	
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    /*
     Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
     Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
     */
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    /*
     Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
     If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
     */
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    /*
     Called as part of  transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
     */
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    /*
     Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     */
}


- (void)applicationWillTerminate:(UIApplication *)application {
    /*
     Called when the application is about to terminate.
     See also applicationDidEnterBackground:.
     */
}


#pragma mark -
#pragma mark Memory management

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
    /*
     Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
     */
}


- (void)dealloc {
    [viewController release];
    [window release];
    [super dealloc];
}


#pragma mark -
#pragma mark - Location Function 
-(void) getCurrentLatLon
{
	locationManager = [[CLLocationManager alloc] init];
	locationManager.delegate = self;
	
	locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
	locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; 
    [locationManager startUpdatingLocation];
}

-(BOOL) CheckLocationService
{
	BOOL IsEnable = TRUE;
#if __IPHONE_OS_VERSION_MIN_REQUIRED > __IPHONE_4_2
	if (!([CLLocationManager locationServicesEnabled] && ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized)))
#else 
		if (![CLLocationManager locationServicesEnabled])	
#endif
		{
			
			IsEnable = FALSE;
			UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location Service Disabled" 
															message:@"To re-enable, please go to Settings and turn on Location Service for this app." 
														   delegate:nil 
												  cancelButtonTitle:@"OK" 
												  otherButtonTitles:nil];
			[alert show];
			[alert release];
		}
	
	
	return IsEnable;
}


#pragma mark -
#pragma mark Location Manager Delegate methods

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{	
	
		
	self.newlocation = newLocation;
	//This condition is used to check the accuracy of recieved location
	if(fabs(newLocation.horizontalAccuracy) <= BestAccuracyRange)
		[locationManager stopUpdatingLocation];
		
	
}

/*
 - (void)locationManager:(CLLocationManager *)manager
 didExitRegion:(CLRegion *)region
 {
 //    @autoreleasepool {
 
 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Hello....." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
 [alert show];
 [alert release];
 //   }
 }
 */
 - (void)locationManager:(CLLocationManager *)manager
 monitoringDidFailForRegion:(CLRegion *)region
 withError:(NSError *)error
 {
	 NSLog(@"Error In Getting User Location");
// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Error....." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
// [alert show]; 
// [alert release];
 }


#pragma mark -
#pragma mark Network Status Methods
- (BOOL)networkAvailable 
{
	NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];
	BOOL IsinternetAvailable=[[Reachability sharedReachability] internetConnectionStatus];
	[pool release];
	return IsinternetAvailable;
	
}



@end
